//
//  LeftViewController.m
//  Ufan
//
//  Created by yangqingluo on 15/10/25.
//  Copyright © 2015年 yangqingluo. All rights reserved.
//

#import "LeftViewController.h"
#import "QLCache.h"
#import "UIView+KGViewExtend.h"
#import "LeftOrderVC.h"
#import "LeftSettingVC.h"
#import "LeftAboutVC.h"

#define cellHeight_big 60
#define cellHeight_middle 50


@interface LeftViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UITableView *showTableView;
    NSArray *showArray;
    
    double cellHeight;
}

@property (nonatomic, retain) UIView *contentView;

@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    showArray = @[@[@{@"title":@"添加小孩",@"subTitle":@"您还没有添加小孩，点击立即添加",@"headImage":@"account_0_0"}],
                  @[@{@"title":@"首页",@"subTitle":@"",@"headImage":@"sidebar_00"},
                    @{@"title":@"订单",@"subTitle":@"",@"headImage":@"sidebar_01"},
                    @{@"title":@"设置",@"subTitle":@"",@"headImage":@"sidebar_02"},
                    @{@"title":@"关于",@"subTitle":@"",@"headImage":@"sidebar_03"},
                    @{@"title":@"退出登录",@"subTitle":@"",@"headImage":@"sidebar_04"}]
                  ];
    
    _contentView = [[UIView alloc] initWithFrame:self.view.bounds];
    _contentView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_contentView];
    NSLog(@"%f", _contentView.layer.anchorPoint.x);
    
    
    showTableView= [[UITableView alloc] initWithFrame:CGRectMake(0, 50, _contentView.bounds.size.width, _contentView.bounds.size.height - 100) style:UITableViewStyleGrouped];
    showTableView.multipleTouchEnabled = YES;
    showTableView.dataSource = self;
    showTableView.delegate = self;
    showTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [showTableView setBackgroundColor:[UIColor clearColor]];
    showTableView.sectionHeaderHeight = 0.0;
    [self.view addSubview:showTableView];
}


#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [showArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *array = showArray[section];
    
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *reuseIdetify = @"left";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdetify];;
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdetify];
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell.textLabel setTextColor:[UIColor whiteColor]];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    if (indexPath.section == 0) {
        UIImageView *headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 60, 60)];
        headerImageView.layer.cornerRadius = 0.5 * headerImageView.bounds.size.width;
        headerImageView.layer.masksToBounds = YES;
        
        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
        NSMutableDictionary *userInfoDic = [QLCache objectForKey:[defaults objectForKey:@"username"]];
        UIImage *head_image = [UIImage imageWithData:[userInfoDic objectForKey:@"userimage"]];
        if (head_image) {
            headerImageView.image = head_image;
        }
        else{
            headerImageView.image = [UIImage imageNamed:@"user_head"];
        }
        
        [cell.contentView addSubview:headerImageView];
        
        
        UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(headerImageView.right + 20, 10, 200, 30)];
        name.textColor = [UIColor whiteColor];
        name.font = [UIFont systemFontOfSize:18.0];
        name.text = [defaults objectForKey:@"username"];
        [cell.contentView addSubview:name];
        
        UILabel *uuid = [[UILabel alloc]initWithFrame:CGRectMake(headerImageView.right + 20, 40, 200, 20)];
        uuid.textColor = [UIColor grayColor];
        uuid.font = [UIFont systemFontOfSize:14.0];
        uuid.text = [defaults objectForKey:@"hostuuid"];
        [cell.contentView addSubview:uuid];
        
        
    }
    else if (indexPath.section == 1) {
        NSArray *array = showArray[indexPath.section];
        
        NSDictionary *dic = array[indexPath.row];
        
//        cell.imageView.image = [UIImage imageNamed:[dic objectForKey:@"headImage"]];
        cell.textLabel.text = [dic objectForKey:@"title"];
        
    }
    
    
    
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 80;
    }
    else if (indexPath.section == 1) {
        cellHeight = self.view.height > 500 ? cellHeight_big : cellHeight_middle;
        
        return cellHeight;
    }
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        switch (indexPath.row) {
                //订单
            case 1:{
                if ([AppPublic shareInstanceApp].Token) {
                    [AppPublic shareInstanceApp].needRefreshOrderList = YES;
                    LeftOrderVC *orderVC = [LeftOrderVC new];
                    
                    [[AppPublic shareInstanceApp].mainSlide.mainVC pushViewController:orderVC animated:NO];
                    [[AppPublic shareInstanceApp].mainSlide showMainView];
                }
                else{
                    [[AppPublic shareInstanceApp] presentLoginVC:[AppPublic shareInstanceApp].mainSlide.mainVC];
                    [[AppPublic shareInstanceApp].mainSlide showMainView];
                }
            }
                break;
                
                //设置
            case 2:{
                LeftSettingVC *settingVC = [LeftSettingVC new];
                
                [[AppPublic shareInstanceApp].mainSlide.mainVC pushViewController:settingVC animated:NO];
                [[AppPublic shareInstanceApp].mainSlide showMainView];
                
            }
                break;
                
                //关于
            case 3:{
//                if ([AppPublic shareInstanceApp].mainViewIndexNow == 1) {
//                    [[AppPublic shareInstanceApp] showMainViewWithIndex:2];
//                }
//                else if ([AppPublic shareInstanceApp].mainViewIndexNow == 2) {
//                    [[AppPublic shareInstanceApp] showMainViewWithIndex:1];
//                }
                LeftAboutVC *aboutVC = [LeftAboutVC new];
                [[AppPublic shareInstanceApp].mainSlide.mainVC pushViewController:aboutVC animated:NO];
                [[AppPublic shareInstanceApp].mainSlide showMainView];
            }
                break;
                
                //退出登录
            case 4:{
                
                
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"确定退出?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                [alert show];
                
            }
                break;
                
            default:
                break;
        }
    }
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        //退出登录
        [AppPublic doLogout];
    }

}

@end
